package com.mallcai.bigdata.ladon.controller.common;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mallcai.bigdata.ladon.controller.common.form.CommonInfoForm;
import com.mallcai.bigdata.ladon.dpl.entity.common.CommonInfo;
import com.mallcai.bigdata.ladon.dpl.vo.CommonInfoType;
import com.mallcai.bigdata.ladon.service.common.ICommonInfoService;
import com.mallcai.bigdata.ladon.vo.MyPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-05-22 10:27.
 * Modify:
 */
@RestController
@RequestMapping("/common/info")
public class CommonInfoController {

    @Autowired
    private ICommonInfoService iCommonInfoService;

    @RequestMapping("/add")
    public String addDatasource(@RequestBody @Validated(value = CommonInfoForm.AddCheck.class) CommonInfoForm form) {
        CommonInfo ci = form.toEntity();
        int status = iCommonInfoService.add(ci);
        if (status > 0) {
            return "添加成功";
        } else {
            throw new BusinessException("添加失败");
        }
    }

    @RequestMapping("/edit")
    public String editDatasource(@RequestBody @Validated(value = CommonInfoForm.EditCheck.class) CommonInfoForm form) {
        CommonInfo ci = form.toEntity();
        int status = iCommonInfoService.edit(ci);
        if (status > 0) {
            return "修改成功";
        } else {
            throw new BusinessException("修改失败");
        }
    }

    @RequestMapping("/del")
    public String delDatasource(@RequestBody @Validated(value = CommonInfoForm.IdCheck.class) CommonInfoForm form) {
        CommonInfo ci = form.toEntity();
        int status = iCommonInfoService.delete(ci);
        if (status > 0) {
            return "删除成功";
        } else {
            throw new BusinessException("删除失败");
        }
    }

    @RequestMapping("/getById")
    public CommonInfo getById(@RequestBody @Validated(value = CommonInfoForm.IdCheck.class) CommonInfoForm form) {
        return iCommonInfoService.getById(form.getCommonInfoId());
    }

    @RequestMapping("/getByPage")
    public MyPageInfo getByPage(@RequestBody @Validated CommonInfoForm form) {
        PageHelper.startPage(form.getCurrentPage(), form.getPageSize());
        List<CommonInfo> commonInfos = iCommonInfoService.getByParams(form.getParams());
        PageInfo<CommonInfo> pageInfo = new PageInfo<>(commonInfos);
        return new MyPageInfo<>(pageInfo);
    }

    /**
     * 根据分类获取列表
     *
     * @param form CommonInfoForm
     * @return List
     */
    @RequestMapping("/getByClassify")
    public List<CommonInfo> getByClassify(@RequestBody @Validated(value = CommonInfoForm.ClassifyCheck.class) CommonInfoForm form) {
        return iCommonInfoService.getByClassify(form.getClassify());
    }


    /**
     * 获取所有类型
     *
     * @return ApiResult
     */
    @RequestMapping("/getTypes")
    public List<Map<String,String>> getDataTypes() {
        List<Map<String, String>> types = new ArrayList<>();

        for (CommonInfoType dt : CommonInfoType.values()) {
            Map<String,String> map = new HashMap<>();
            map.put("classify", dt.getCode());
            map.put("desc", dt.getDesc());
            map.put("parentClassify", dt.getParentCode());
            types.add(map);
        }
        return types;
    }

    @RequestMapping("/getDepartment")
    public List<CommonInfo> getByClassify() {
        List<CommonInfo> commonInfos = iCommonInfoService.getByClassify(CommonInfoType.DEPARTMENT_INFO.getCode());
        commonInfos.sort(Comparator.comparing(d -> Integer.valueOf(d.getCode())));
        return commonInfos;
    }
}
